package com.topway.dsmflex.ui.composite.synchro;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.Application;
import com.swtdesigner.SWTResourceManager;
import com.topway.dsmflex.Log;
import com.topway.dsmflex.model.synchro.DataFileBean;
import com.topway.dsmflex.model.synchro.DataSourceBean;
import com.topway.dsmflex.services.synchro.FileServices;
import com.topway.dsmflex.ui.InitFormComposite;
import com.topway.dsmflex.ui.contentProvider.TableContentProvider;
import com.topway.dsmflex.ui.labelProvider.FileNameProvider;
import com.topway.dsmflex.ui.labelProvider.SqlNameProvider;

/**
 * 文件同步
 * 
 * @author liusy 2013-07-16
 * 
 */
public class FileSynchro extends InitFormComposite {

	public static String name = "同步文件设置";

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(FileSynchro.class);

	private Shell shell;
	private Table tblTab;
	private CheckboxTableViewer tvTab;
	private TableColumn colOperator;
	private DataSourceBean dsSource;
	private DataFileBean dSql;
	private FileServices fileServices = null;
	public List<DataFileBean> dSqlList  = null;

	public FileSynchro(Composite parent, int style) {
		super(parent, style);
		createContents();
		initForm();
	}

	public int initForm() {
		this.fileServices = FileServices.getFileServices();
		dSqlList = fileServices.getAllDataTable(dsSource);
		if (null != dSqlList && dSqlList.size() > 0)
			tvTab.setInput(dSqlList);
		for (int i = 0; i < dSqlList.size(); i++) {
			if (dSqlList.get(i).getCheck() == 1)
				tvTab.setChecked(dSqlList.get(i), true);
		}
		return 1;
	}

	protected void createContents() {

		shell = this.getShell();
		setLayout(new FormLayout());
		Button btnSvae = new Button(this, SWT.NONE);
		FormData fd_btnSvae = new FormData();
		fd_btnSvae.bottom = new FormAttachment(100, -5);
		fd_btnSvae.left = new FormAttachment(50, -30);
		btnSvae.setSize(30, 50);
		btnSvae.setLayoutData(fd_btnSvae);
		// btnSvae.setAlignment(SWT.LEFT);
		btnSvae.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				Object[] obj = (Object[]) tvTab.getCheckedElements();
				List<DataFileBean> list = new ArrayList<DataFileBean>();
				for (int i = 0; i < obj.length; i++) {
					list.add((DataFileBean) obj[i]);
				}
				fileServices.saveTableConfigList(list);
				if (list.size() > 0) {
					Log.clearLog();
					// Alert.MsgBox("提示", "保存成功");
					Application.openMenu(SystemLog.name);
				}
			}
		});

		btnSvae.setImage(SWTResourceManager.getImage(Application.class,
				"/img/savesetting.gif"));
		btnSvae.setFont(SWTResourceManager.getFont("", 10, SWT.BOLD));
		btnSvae.setText("编译");
		btnSvae.setToolTipText("编译并保存，如未通过，日志将以红色字体提示");

		tvTab = CheckboxTableViewer.newCheckList(this, SWT.BORDER
				| SWT.FULL_SELECTION);
		tvTab.setContentProvider(new TableContentProvider());
		tvTab.setLabelProvider(new FileNameProvider());
		tblTab = tvTab.getTable();
		FormData fd_tblTab = new FormData();
		fd_tblTab.bottom = new FormAttachment(93, 0);
		fd_tblTab.top = new FormAttachment(0, 0);
		fd_tblTab.left = new FormAttachment(0, 0);
		fd_tblTab.right = new FormAttachment(100, 0);
		tblTab.setLayoutData(fd_tblTab);
		tblTab.setLinesVisible(true);
		tblTab.setHeaderVisible(true);
		TableColumn colTabCheck = new TableColumn(tblTab, SWT.CHECK);
		colTabCheck.setWidth(20);

		TableColumn name = new TableColumn(tblTab, SWT.NONE);
		name.setWidth(150);
		name.setText("名称");

		TableColumn statu = new TableColumn(tblTab, SWT.NONE);
		statu.setWidth(50);
		statu.setText("激活");

		colOperator = new TableColumn(tblTab, SWT.NONE);
		colOperator.setWidth(80);
		colOperator.setText("目标操作");

		TableColumn targetSource_1 = new TableColumn(tblTab, SWT.NONE);
		targetSource_1.setWidth(80);
		targetSource_1.setText("目标数据源");

		TableColumn targetTable = new TableColumn(tblTab, SWT.NONE);
		targetTable.setWidth(125);
		targetTable.setText("目标表");

		TableColumn synchroNterval = new TableColumn(tblTab, SWT.NONE);
		synchroNterval.setWidth(70);
		synchroNterval.setText("频率(分)");

		TableColumn targetExecuteSql = new TableColumn(tblTab, SWT.NONE);
		targetExecuteSql.setWidth(80);
		targetExecuteSql.setText("目标后行");
		
		TableColumn sourceOperator = new TableColumn(tblTab, SWT.NONE);
		sourceOperator.setWidth(80);
		sourceOperator.setText("源操作");

		// 添加托盘右键菜单
		final Menu menu = new Menu(shell, SWT.POP_UP);
		final MenuItem menuAdd = new MenuItem(menu, SWT.PUSH);// 开始
		menuAdd.setText("添加");
		menuAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				AddFileSynchro addFileSynchro = new AddFileSynchro(shell
						.getShell(), SWT.NONE, null);
				dSql = (DataFileBean) addFileSynchro.open();
				if (null == dSql)
					return;
				tvTab.add(dSql);
				tvTab.setChecked(dSql, true);
			}
		});
		final MenuItem menuUpdate = new MenuItem(menu, SWT.PUSH);// 开始
		menuUpdate.setText("修改");
		menuUpdate.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				IStructuredSelection obj = (IStructuredSelection) tvTab
						.getSelection();
				dSql = (DataFileBean) obj.getFirstElement();
				if (null == dSql)
					return;
				AddFileSynchro addFileSynchro = new AddFileSynchro(shell
						.getShell(), SWT.NONE, dSql);
				DataFileBean _dSql = (DataFileBean) addFileSynchro.open();
				if (null != _dSql) {
//					tvTab.remove(dSql);
//					dSql = _dSql;
//					tvTab.add(dSql);
//					tvTab.setChecked(dSql, true);
					 tvTab.refresh();
				}

			}
		});
		final MenuItem menuDelete = new MenuItem(menu, SWT.PUSH);
		menuDelete.setText("删除");
		menuDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				IStructuredSelection obj = (IStructuredSelection) tvTab
						.getSelection();
				dSql = (DataFileBean) obj.getFirstElement();
				if (null == dSql)
					return;
				tvTab.remove(dSql);
			}
		});
		tblTab.setMenu(menu);

		tvTab.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent arg0) {
				IStructuredSelection obj = (IStructuredSelection) arg0
						.getSelection();
				dSql = (DataFileBean) obj.getFirstElement();
				AddFileSynchro addFileSynchro = new AddFileSynchro(shell
						.getShell(), SWT.NONE, dSql);
				DataFileBean _dSql = (DataFileBean) addFileSynchro.open();
				if (null != _dSql) {
					tvTab.remove(dSql);
					dSql = _dSql;
					tvTab.add(dSql);
					tvTab.setChecked(dSql, true);
				}
			}
		});
	}

}
